//
//  NEDPopBaseView.m
//  MoreCoin
//
//  Created by 林宁宁 on 2019/9/23.
//  Copyright © 2019 MoreCoin. All rights reserved.
//

#import "NEDPopBaseView.h"
#import "NSObject+Keyboard.h"
#import "UIColor+NEDStyle.h"
#import "NEDConstant.h"

@interface NEDPopBaseView()

@property (retain, nonatomic, readwrite) UIView * backgroundView;

@end

@implementation NEDPopBaseView

-(void)ned_commonInit{
    self.tapBackgroundToClose = YES;
    
    self.backgroundView = [UIView new];
    self.backgroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
    self.backgroundView.alpha = 0;
    [self addSubview:self.backgroundView];
    [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    self.contentView = [UIView new];
    self.contentView.backgroundColor = [UIColor ned_background];
    self.contentView.layer.cornerRadius = 10;
    self.contentView.layer.masksToBounds = YES;
    [self addSubview:self.contentView];
    
    [self ned_setupUI];
    
    [self ned_keyboardShowAtAnimationView:self.contentView];
}

- (void)popShow{
    [self popShowAtView:nil];
}

- (void)popShowAtView:(UIView * _Nullable)aimView{
    if (self.superview) {
        [self removeFromSuperview];
    }
    UIView * superV = aimView?:[UIApplication sharedApplication].keyWindow;
    self.frame = superV.bounds;
    [superV addSubview:self];
    [self mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(superV);
    }];
    
    self.backgroundView.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundView.alpha = 1;
    } completion:^(BOOL finished) {
        if(self.backgroundViewDidShow){
            self.backgroundViewDidShow();
        }
    }];
    
    [self ned_setupData];
    [self ned_popShowAnimation];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ned_tapBgEvent)];
    [self.backgroundView addGestureRecognizer:tap];
    
    if(self.didShowBlock){
        self.didShowBlock();
    }
}

- (void)popDismiss{
    [self ned_popDismissAnimation];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
        self.contentView.alpha = 0;
        self.closeBtn.alpha = 0;
    } completion:^(BOOL finished) {
        if(finished){
            [self removeFromSuperview];
        }
        if(self.didCloseBlock){
            self.didCloseBlock();
        }
    }];
}

#pragma mark - close btn

- (void)clickCloseEvent{
    if(self.clickCloseBlock){
        self.clickCloseBlock();
    }
    [self popDismiss];
}

/// 右上角独立于contentView的关闭按钮
- (void)addCloseBtn:(NEDVoidBlock)action closeImage:(UIImage * _Nullable)closeImage{
    
    //关闭按钮
    UIButton * closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.closeBtn = closeBtn;
    
    closeBtn.adjustsImageWhenDisabled = NO;
    closeBtn.adjustsImageWhenHighlighted = NO;
    
    [closeBtn setImage:closeImage?:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    if(action){
        self.clickCloseBlock = action;
    }
    [closeBtn addTarget:self action:@selector(clickCloseEvent) forControlEvents:UIControlEventTouchUpInside];
    
    closeBtn.backgroundColor = [UIColor clearColor];
    [self addSubview:closeBtn];
    
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(Adapted(40), Adapted(40)));
    }];
}

#pragma mark - sub
- (void)ned_setupUI{
    
}

- (void)ned_popShowAnimation{
    //设置毛玻璃效果
    if(self.isBlurShow){
        self.backgroundView.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor ned_background];
        UIBlurEffect * blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
        UIVisualEffectView * effectV = [[UIVisualEffectView alloc] initWithEffect:blur];
        effectV.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
        effectV.alpha = 0.9;
        [self insertSubview:effectV atIndex:0];
        [effectV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    
    self.contentView.transform = CGAffineTransformMakeScale(0.8, 0.8);
    self.closeBtn.transform = CGAffineTransformMakeScale(0.5, 0.5);
    CGFloat duration = 0.3;
    if (self.popFromBottom) {
        self.contentView.transform = CGAffineTransformMakeTranslation(0, 400);
        duration = 0.5;
    }
    [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.contentView.transform = CGAffineTransformIdentity;
        self.closeBtn.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [self ned_popShowFinish];
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration*0.9 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self ned_popShowWillFinish];
        if(self.willAppearShow){
            self.willAppearShow();
        }
    });
}

- (void)ned_popShowFinish{
    
}
- (void)ned_popShowWillFinish{
    
}

- (void)ned_popDismissAnimation{
    if (self.popFromBottom) {
        self.contentView.transform = CGAffineTransformIdentity;
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.contentView.transform = CGAffineTransformMakeTranslation(0, 400);
        } completion:^(BOOL finished) {
            
        }];
    }
}

- (void)ned_setupData{
    
}

- (void)ned_tapBgEvent{
    if(self.tapBackgroundToClose){
        [self popDismiss];
    }
}

//+ (void)show{
//    NEDPopBaseView * popV = [self new];
//    [popV popShow];
//}

+ (void)cleanPopView{
    for(UIView * subV in [UIApplication sharedApplication].keyWindow.subviews){
        if([subV isKindOfClass:[NEDPopBaseView class]]){
            NEDPopBaseView * popV = (NEDPopBaseView *)subV;
            if(!popV.isAlwaysStay){
                [popV popDismiss];
            }
        }
    }
}

@end
